package com.company.cloud.uba.modules.app.service.impl;

import cn.hutool.core.lang.Dict;
import com.alibaba.fastjson.JSON;
import com.company.cloud.article.modules.api.response.ArticleReap;
import com.company.cloud.article.modules.api.service.ArticleClientService;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.page.modules.api.response.PageCardConfigResponse;
import com.company.cloud.page.modules.api.service.PageClientService;
//import com.company.cloud.poster.modules.api.service.PosterClientService;
import com.company.cloud.product.modules.api.response.Product;
import com.company.cloud.product.modules.api.service.ProductRemoteService;
//import com.company.cloud.train.modules.api.service.TrainClientService;
import com.company.cloud.uba.modules.app.models.RelDataResp;
import com.company.cloud.uba.modules.app.service.UbaRelDataService;
import com.company.cloud.uba.modules.base.entity.UserBrowseObjLog;
import com.company.cloud.user.modules.api.response.UserSimple;
import com.company.cloud.user.modules.api.service.UserRemoteService;
import com.company.common.server.constant.IRelBusiType;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Log4j2
@Service
public class UbaRelDataServiceImpl implements UbaRelDataService {


    @Autowired
    private ArticleClientService articleClientService;
    //    @Autowired
//    private TrainClientService trainClientService;
//    @Autowired
//    private PosterClientService posterClientService;
    @Autowired
    private ProductRemoteService productRemoteService;
    @Autowired
    private UserRemoteService userRemoteService;
    @Autowired
    private PageClientService pageClientService;


    @Override
    public RelDataResp getBrowseObjLogRelData(List<UserBrowseObjLog> logs) {
        RelDataResp data = new RelDataResp();
        Dict dict = this.getRelIdsMap(logs);
        Map<String, Set<String>> relIdMap = (Map<String, Set<String>>) dict.get("relIdMap");
        Set<Integer> userIds = (Set<Integer>) dict.get("userIds");

        for (String relIdType : relIdMap.keySet()) {
            Set<String> relIds = relIdMap.get(relIdType);
            if (!CollectionUtils.isEmpty(relIds)) {
                if (IRelBusiType.PRD_Product.equals(relIdType)) {
                    BaseClientResult<Map<String, Product>> productMapResult = productRemoteService.getProductMapByDbByIds(relIds);
                    data.setProductMap(BaseClientResult.getResultData(productMapResult));
                }
            }

            if (IRelBusiType.CMS_Article.equals(relIdType)) {
                System.out.println("+++++++ type=" + IRelBusiType.CMS_Article);
                System.out.println(JSON.toJSONString(relIds));
                BaseClientResult<Map<String, ArticleReap>> articleResp = articleClientService.getCompanyArticleMapByNos(relIds);
                Map<String, ArticleReap> articleMap = BaseClientResult.getResultData(articleResp);
                if (articleMap != null) {
                    System.out.println("size=" + articleMap.keySet().size() + "____" + JSON.toJSONString(articleMap.keySet()));
                }
                data.setArticleMap(articleMap);
            }

            if (IRelBusiType.CMS_Course.equals(relIdType)) {
//                trainClientService.
//                CourseClientModel rel = trainClientService.getCourseClientModel(model.getObjNo());
            }

            if (IRelBusiType.CMS_Poster.equals(relIdType)) {
//                PosterCompanyModel4Client rel = posterClientService.getPosterCompanyByCache(model.getObjNo());
            }

            if (IRelBusiType.CMS_PageCard.equals(relIdType)) {
                BaseClientResult<Map<String, PageCardConfigResponse>> pageCardMap = pageClientService.getPageCardConfigMap(relIds);
                data.setPageCardMap(BaseClientResult.getResultData(pageCardMap));
            }

        }


        if (!CollectionUtils.isEmpty(userIds)) {
            BaseClientResult<Map<Integer, UserSimple>> userMapResult = userRemoteService.getUserMapByIds(userIds);
            data.setUserMap(BaseClientResult.getResultData(userMapResult));
        }
        return data;
    }

    private Dict getRelIdsMap(List<UserBrowseObjLog> logs) {
        Map<String, Set<String>> relIdMap = new HashMap<>();
        Set<Integer> userIds = new HashSet<>();

        logs.forEach(item -> {
            Set<String> ids = relIdMap.get(item.getRelIdType());
            if (ids == null) {
                ids = new HashSet<>();
                relIdMap.put(item.getRelIdType(), ids);
            }
            ids.add(item.getRelId());
            if (CheckUtils.hasInt(item.getUserId())) {
                userIds.add(item.getUserId());
            }
        });
        Dict dict = Dict.create();
        dict.set("userIds", userIds);
        dict.set("relIdMap", relIdMap);
        return dict;
    }


}
